#include "stdafx.h"
#include "wf_selectintersected.h"

#include "lx_define.h"
#include "pf_snapper.h"
#include "pf_selection.h"



/**
 * Constructor.
 *
 * @param select true: select window. false: deselect window
 */
WF_SelectIntersected::WF_SelectIntersected(
    PF_Container& container,
    PF_GraphicView& graphicView,
    bool select)
        : PF_PreviewActionInterface("Select Intersected",
                            container, graphicView) {

    this->select = select;
}


QAction* WF_SelectIntersected::createGUIAction(LX_Define::ActionType type, QObject* /*parent*/) {
    QAction* action;
    if (type==LX_Define::ActionSelectIntersected) {
        action = new QAction(tr("Select Intersected Entities"),  NULL);
		action->setIcon(QIcon(":/extui/selectinters.png"));
        //action->zetStatusTip(tr("Selects all entities intersected by a line"));
    } else {
        action = new QAction(tr("Deselect Intersected Entities"), NULL);
		action->setIcon(QIcon(":/extui/deselectinters.png"));
        //action->zetStatusTip(tr("Deselects all entities intersected by a line"));
    }
    return action;
}


void WF_SelectIntersected::init(int status) {
    PF_PreviewActionInterface::init(status);

    v1 = v2 = PF_Vector(false);
    snapMode = LX_Define::SnapFree;
    snapRes = LX_Define::RestrictNothing;
}



void WF_SelectIntersected::trigger() {
    PF_PreviewActionInterface::trigger();

    if (v1.valid && v2.valid) {
        if (graphicView->toGuiDX(v1.distanceTo(v2))>10) {

            PF_Selection s(*container, graphicView);
            s.selectIntersected(v1, v2, select);

            if (PF_DIALOGFACTORY!=NULL) {
                PF_DIALOGFACTORY->updateSelectionWidget(container->countSelected());
            }

            init();
        }
    }
}



void WF_SelectIntersected::mouseMoveEvent(PF_MouseEvent* e) {
    if (getStatus()==SetPoint2 && v1.valid) {
        v2 = snapPoint(e);
        deletePreview();
        preview->addEntity(new PF_Line(preview,
                                       PF_LineAttribute(PF_Vector(v1.x, v1.y),
                                                   PF_Vector(v2.x, v2.y))));
        drawPreview();
    }
}



void WF_SelectIntersected::mousePressEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        switch (getStatus()) {
        case SetPoint1:
            v1 = snapPoint(e);
            setStatus(SetPoint2);
            break;

        default:
            break;
        }
    }

    PF_DEBUG->print("PF_ActionSelectIntersected::mousePressEvent(): %f %f",
                    v1.x, v1.y);
}



void WF_SelectIntersected::mouseReleaseEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("PF_ActionSelectIntersected::mouseReleaseEvent()");
    if (e->button()==Qt::RightButton) {
        if (getStatus()==SetPoint2) {
            deletePreview();
        }
        init(getStatus()-1);
    } else if (e->button()==Qt::LeftButton) {
        if (getStatus()==SetPoint2) {
            v2 = snapPoint(e);
            trigger();
        }
    }
}



void WF_SelectIntersected::updateMouseButtonHints() {
    if (PF_DIALOGFACTORY!=NULL) {
        switch (getStatus()) {
        case SetPoint1:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Choose first point of intersection line"), tr("Cancel"));
            break;
        case SetPoint2:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Choose second point of intersection line"), tr("Back"));
            break;
        default:
            PF_DIALOGFACTORY->updateMouseWidget("", "");
            break;
        }
    }
}



void WF_SelectIntersected::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::SelectCursor);
}



void WF_SelectIntersected::updateToolBar() {
    if (PF_DIALOGFACTORY!=NULL) {
        if (!isFinished()) {
            //PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
            PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSelect);
        } else {
            PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSelect);
        }
    }
}

// EOF
